<?php
include_once 'entity/UserSystem.php';
include_once 'entity/Role.php';
class Administrator_UserSystem_Grid extends BaseGrid{
	protected $seachEntity = array(
		UserSystem::ID,
		UserSystem::ACCOUNT,
		UserSystem::NAME,
		UserSystem::ROLE,
		UserSystem::STATUS
	);
	/**
	 * (non-PHPdoc)
	 * @see library/Grid/BaseGrid::init()
	 */
	public function init(){
		$this->headerLb[UserSystem::ID] = 'ID'; 
		$this->headerLb[UserSystem::ACCOUNT] = 'Tài khoản';
		$this->headerLb[UserSystem::ADDRESS] = 'Địa chỉ';
		$this->headerLb[UserSystem::BIRTHDAY] = 'Ngày sinh';
		$this->headerLb[UserSystem::EMAIL] = 'Email';
		$this->headerLb[UserSystem::GENDER] = 'Giới tính';
		$this->headerLb[UserSystem::IMAGE] = 'Hình ảnh';
		$this->headerLb[UserSystem::NAME] = 'Tên';
		$this->headerLb[UserSystem::PASSWORD] = 'Mật khẩu';
		$this->headerLb[UserSystem::PHONE] = 'Số điện thoại';
		$this->headerLb[UserSystem::ROLE] = 'Phân quyền';
		$this->headerLb[BaseEntity::ROWVERSION] = 'Version';
		$this->headerLb[BaseEntity::LASTUPDATEBY] = 'Cập nhật bởi';
	}
	/**
	 * (non-PHPdoc)
	 * @see library/Grid/BaseGrid::_preRender()
	 */
	public function _preRender(){
		$link = $this->getEditLink();
		$this->cols = array(
			BaseGrid::COL_ORDINAL => array(BaseGrid::TYPE_ORDINAL),
			BaseGrid::COL_CHECKBOX => array(BaseGrid::TYPE_CHECKBOX),
			UserSystem::ACCOUNT => array(UserSystem::ACCOUNT,BaseGrid::TYPE_LINK,$link,array(UserSystem::ID)),
			UserSystem::NAME => array(UserSystem::NAME,BaseGrid::TYPE_TEXT),
			UserSystem::EMAIL => array(UserSystem::EMAIL,BaseGrid::TYPE_TEXT),
			UserSystem::BIRTHDAY => array(UserSystem::BIRTHDAY,BaseGrid::TYPE_TEXT),
//			UserSystem::LASTUPDATEBY => array(UserSystem::LASTUPDATENAME,BaseGrid::TYPE_TEXT),
//			UserSystem::LASTUPDATETIME => array(UserSystem::LASTUPDATETIME,BaseGrid::TYPE_TEXT),
			UserSystem::ROLE => array(UserSystem::ROLENAME,BaseGrid::TYPE_TEXT,'role',Role::ID,Role::NAME,UserSystem::ROLENAME),
//			UserSystem::ROWVERSION => array(UserSystem::ROWVERSION,BaseGrid::TYPE_TEXT),
			UserSystem::STATUS => array(UserSystem::STATUSNAME,BaseGrid::TYPE_TEXT),
			UserSystem::ID => array(UserSystem::ID,BaseGrid::TYPE_TEXT)
		);
	}
	public function searchQuery(){
		$sql = 'select t.' .UserSystem::ID.',';
		$sql .= 't.' .UserSystem::ACCOUNT.',';
		$sql .= 't.' .UserSystem::NAME.',';
		$sql .= 't.' .UserSystem::EMAIL.',';
		$sql .= 't.' .UserSystem::BIRTHDAY.',';
		$sql .= 't.' .UserSystem::LASTUPDATETIME.',';
		$sql .= 't.' .UserSystem::ROWVERSION.',';
		$sql .= 'r.name as '.UserSystem::ROLENAME.',';
		$sql .= 's.name as '.UserSystem::STATUSNAME;
		$sql .= ' from usersystem as t';
		$sql .= ' left join role as r';
		$sql .= ' on t.role = r.id';
		$sql .= ' left join status as s';
		$sql .= ' on t.status = s.id';
		$where = " where ";
		if(isset($this->searchCondition['UserSystem_id']) && $this->searchCondition['UserSystem_id'] != "")
		$where .= ' t.id="'.$this->searchCondition['UserSystem_id'].'" and';
		
		if(isset($this->searchCondition['UserSystem_account']) && $this->searchCondition['UserSystem_account'] != "")
		$where .= ' t.account like"%'.$this->searchCondition['UserSystem_account'].'%" and';
		
		if(isset($this->searchCondition['UserSystem_name']) && $this->searchCondition['UserSystem_name'] != "")
		$where .= ' t.name like"%'.$this->searchCondition['UserSystem_name'].'%" and';
		
		if(isset($this->searchCondition['UserSystem_role']) && $this->searchCondition['UserSystem_role'] != "")
		$where .= ' t.role="'.$this->searchCondition['UserSystem_role'].'" and';
		
		if(isset($this->searchCondition['UserSystem_status']) && $this->searchCondition['UserSystem_status'] != "")
		$where .= ' t.status="'.$this->searchCondition['UserSystem_status'].'" and';
		
		$where .= ' 1 = 1';
		
		$sql .= $where;
		
		if(isset($this->searchCondition[BaseGrid::ORDERBY_COL]) 
		&& $this->searchCondition[BaseGrid::ORDERBY_COL] != ""
		&& isset($this->searchCondition[BaseGrid::ORDERBY_WITH]) 
		&& $this->searchCondition[BaseGrid::ORDERBY_WITH] != ""){
			$sql .= ' order by '.$this->searchCondition[BaseGrid::ORDERBY_COL];
			$sql .= ' '.$this->searchCondition[BaseGrid::ORDERBY_WITH];
		}
		return $sql;
	}
	public function getEditLink(){
		return $this->getContext()->getBaseUrl()."Administrator/UserSystem/edit/default.html?id={0}";
	}
}